//
//  TestShadowViewController.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2025/7/3.
//  Copyright © 2025 moon. All rights reserved.
//

#import "TestShadowViewController.h"
#import "MKUIElementListViewCellSwitch+Test.h"
#import "MKUIElementListViewCellSelection+Test.h"
#import "MKUIElementListViewCellButton+Test.h"
#import "TestMKUIElementViewSlider.h"
#import "TestElementHeaders.h"
@interface TestShadowElementView : MKUIListViewSubViewBase
@property(nonatomic,strong) UIView *outView;
@property(nonatomic,strong) UIView *view;
@property(nonatomic,strong) UIView *centerView;
@property(nonatomic,assign) BOOL maskViewFlag;
@property(nonatomic,strong) MKUIFlowLayoutConstraint *flowlayout;
@end
@implementation TestShadowElementView
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.outView = [[UIView alloc] init];
        self.outView.backgroundColor = UIColor.grayColor;
        self.outView.layer.borderColor = UIColor.grayColor.CGColor;
        self.outView.layer.borderWidth = 1;
        [self.contentView addSubview:self.outView];
        
        self.view = [[UIView alloc] init];
        self.view.backgroundColor = UIColor.lightTextColor;
        self.view.layer.cornerRadius = 10;
        self.view.clipsToBounds = NO;
        self.view.layer.borderColor = UIColor.blackColor.CGColor;
        self.view.layer.borderWidth = 1;
        self.view.layer.shadowColor = UIColor.redColor.CGColor;
        self.view.layer.shadowOffset = CGSizeMake(20, 20);
        self.view.layer.shadowOpacity = 0.5;
        self.view.layer.shadowRadius = 0;
        [self.outView addSubview:self.view];
        //
        self.centerView = [[UIView alloc] init];
        self.centerView.backgroundColor = UIColor.blueColor;
        [self.view addSubview:self.centerView];
        
        MKUILayoutConstraintItemWrapper *w1 = [MKUILayoutConstraintItemWrapper wrapItem:self.view fixedSize:CGSizeMake(44, 44)];
        self.flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[w1] constraintParam:(MKUIFlowLayoutConstraintParam_H_C_C) contentInsets:MKUIEdgeInsetsMakeSameEdge(0) interitemSpacing:10];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect contentBounds = self.contentView.bounds;
    CGRect f1 = contentBounds;
    f1.size = CGSizeMake(50, 50);
    MKCGRectAlignMidXToRect(&f1, contentBounds);
    MKCGRectAlignMidYToRect(&f1, contentBounds);
    self.outView.frame = f1;
    
    CGRect bounds = self.outView.bounds;
    self.flowlayout.bounds = bounds;
    [self.flowlayout layoutItemsWithResizeItems:YES];
    
    if(self.maskViewFlag){
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.view.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(15, 15)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc]init];
        maskLayer.frame = self.view.bounds;
        maskLayer.path = maskPath.CGPath;
        self.view.layer.mask = maskLayer;
    }else{
        self.view.layer.mask = nil;
    }
    {
        CGRect bounds = self.view.bounds;
        CGRect f1 = bounds;
        f1.size.width /= 2;
        f1.size.height /= 2;
        MKCGRectAlignMidXToRect(&f1, bounds);
        MKCGRectAlignMidYToRect(&f1, bounds);
        self.centerView.frame = f1;
    }
}
- (void)setMaskViewFlag:(BOOL)maskViewFlag{
    if(_maskViewFlag==maskViewFlag)return;
    _maskViewFlag = maskViewFlag;
    [self setNeedsLayout];
}
- (CGSize)sizeThatFits:(CGSize)size{
    return CGSizeMake(size.width, 100);
}
@end

@interface TestShadowViewController ()
@property(nonatomic,strong) TestShadowElementView *shadowElement;
@end

@implementation TestShadowViewController
+ (MKTFuncItem *)funcItem{
    return [MKTFuncItem funcWithName:@"测试Shadow" viewControllerClass:self];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.listView.style = MKUIListViewStyleInsetGrouped;
    [self.listView addSection];
    self.shadowElement = [[TestShadowElementView alloc] init];
    [self.listView addItem:self.shadowElement];
    @MK_WEAKIFY(self);
    UIView *view = self.shadowElement.view;
    [self.listView addSection];
    {
        MKUIElementListViewCellSwitch *el = [MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"clipsToBounds" on:view.clipsToBounds whenChange:^(BOOL on) {
            view.clipsToBounds = on;
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSwitch *el = [MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"maskViewFlag" on:self.shadowElement.maskViewFlag whenChange:^(BOOL on) {
            @MK_NORMALIZE(self);
            self.shadowElement.maskViewFlag = on;
        }];
        [self.listView addItem:el];
    }
    {
        [self.listView addItem:[TestMKUIElementViewSlider testSliderElementWithTitle:@"cornerRadius" value:view.layer.shadowOpacity minimumValue:0 maximumValue:20 whenChange:^(TestMKUIElementViewSlider * _Nonnull el, CGFloat value) {
            view.layer.cornerRadius = value;
        }]];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testUIColorElementWithTitle:@"borderColor" selected:[UIColor colorWithCGColor:view.layer.borderColor] whenDone:^(UIColor * _Nonnull selectedValue) {
            view.layer.borderColor = selectedValue.CGColor;
        }];
        [self.listView addItem:el];
    }
    MKUIElementListViewCellSelection *backgroundColorEl = nil;
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testUIColorElementWithTitle:@"backgroundColor" selected:view.backgroundColor whenDone:^(UIColor * _Nonnull selectedValue) {
            view.backgroundColor = selectedValue;
        }];
        backgroundColorEl = el;
        [self.listView addItem:el];
    }
    {
        [self.listView addItem:[TestMKUIElementViewSlider testSliderElementWithTitle:@"backgroundColor.alpha" value:0 minimumValue:0 maximumValue:1 whenChange:^(TestMKUIElementViewSlider * _Nonnull el, CGFloat value) {
            view.backgroundColor = [backgroundColorEl.elementView.selectedValue mk_colorWithAlpha:value];
        }]];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testUIColorElementWithTitle:@"shadowColor" selected:[UIColor colorWithCGColor:view.layer.shadowColor] whenDone:^(UIColor * _Nonnull selectedValue) {
            view.layer.shadowColor = selectedValue.CGColor;
        }];
        [self.listView addItem:el];
    }
    {
        [self.listView addItem:[TestMKUIElementViewSlider_CGSize testSliderElementWithTitle:@"shadowOffset" value:view.layer.shadowOffset minimumValue:CGSizeMake(-10, -10) maximumValue:CGSizeMake(20, 20) whenChange:^(TestMKUIElementViewSlider_CGSize * _Nonnull el, CGSize value) {
            view.layer.shadowOffset = value;
        }]];
    }
    {
        [self.listView addItem:[TestMKUIElementViewSlider testSliderElementWithTitle:@"shadowOpacity" value:view.layer.shadowOpacity minimumValue:0 maximumValue:1 whenChange:^(TestMKUIElementViewSlider * _Nonnull el, CGFloat value) {
            view.layer.shadowOpacity = value;
        }]];
    }
    {
        [self.listView addItem:[TestMKUIElementViewSlider testSliderElementWithTitle:@"shadowRadius" value:view.layer.shadowRadius minimumValue:0 maximumValue:10 whenChange:^(TestMKUIElementViewSlider * _Nonnull el, CGFloat value) {
            view.layer.shadowRadius = value;
        }]];
    }
}
@end
